var rkdcx = {
    setBtnStatus:function () {
        var row = $('#rkdcx-grid').datagrid('getSelected');
        if (row.sftzd == 1) {
            $("#btn_rkdcx_confirm").hide();
        } else if (row.sftzd == 0) {
            $("#btn_rkdcx_confirm").show();
        }
    }
}
$(function () {
    //表格初始加载
    $('#rkdcx-grid').datagrid({
        url:$.webPath() + "/jggl/jgdgl/rkdList",
        toolbar:"#rkdcx-toolbar",
        fitColumns:true,
        singleSelect:true,
        pagination:true,
        pageNumber:1,
        showFooter:true,
        fit:true,
        rownumbers:true,
        striped:true,
        columns:[
            [
                {field:'id', title:'id', hidden:true},
                {field:'jgdh', title:'加工单号', align:'center', width:150},
                {field:'khbm', title:'卡号编码', align:'center', width:100},
                {field:'jtwz.code', title:'物资编码', align:'center', width:70},
                {field:'jtwz.name', title:'物资名称', align:'center', width:50},
                {field:'jtwz.ggxh', title:'规格型号', align:'center', width:100},
                {field:'jtwz.dw', title:'单位', align:'center', width:50},
                {field:'cd.z', title:'产地', align:'center', width:50},
                {field:'ck.mc', title:'仓库', align:'center', width:100},
                {field:'sku', title:'标识', align:'center', width:65},
                {field:'jg_sbsl', title:'冲销数量', align:'center', width:50},
                {field:'jg_sbzl', title:'冲销重量', align:'center', width:50},
                {field:"sftzd", title:"状态", align:'center', width:47,
                    formatter:function (value) {
                        if (value == 0)
                            return '已保存';
                        if (value == 1)
                            return '已审核';
                    }
                }
            ]
        ],
        onLoadSuccess:function () {
            $(this).datagrid('selectRow', 0);
            rkdcx.setBtnStatus();
            $(".datagrid-htable .datagrid-cell").css('text-align', 'center');
        },
        onClickRow:function () {
            rkdcx.setBtnStatus();
        },
        onSelect:function (rowIndex, rowData) {
            rkdcx.setBtnStatus();
        }
    });

    //审核按钮
    $("#btn_rkdcx_confirm").click(function () {
        rkdcx_check();
    });
    //查询按钮
    $("#btn_rkdcx_search").click(function () {
        rkdcx_search();
    });
});

//审核
function rkdcx_check() {
    var row = $('#rkdcx-grid').datagrid('getSelected');
    if (row) {
        $.messager.confirm('提示', '是否对该冲销单进行审核？该冲销单冲销量为：' + row.jg_sbzl + '立方米(m³)', function (r) {
            if (r) {
                $.ajax({
                    url:$.webPath() + "/jggl/jgdgl/chang_rkdcx",
                    type:"post",
                    dataType:'text',
                    data:{jgdmx_id:row.id, cxsl:row.jg_sbsl, cxzl:row.jg_sbzl},
                    success:function (data) {
                        var obj = eval('(' + data + ')');
                        if (obj.statusCode == '200') {
                            $('#rkdcx-grid').datagrid('reload');
                            $.messager.show({title:'提示框', msg:obj.message, timeout:3000, showType:'slide'});
                        } else {
                            $('#rkdcx-grid').datagrid('reload');
                            $.messager.show({title:'提示框', msg:obj.message, timeout:3000, showType:'slide'});
                        }
                    },
                    error:function (data) {
                        alert(data);
                    }
                });
            }
        });
    } else {
        $.messager.alert("提示", "请选择要审核的冲销单！", "warning");
    }
}

//加工单查询
function rkdcx_search() {
    var rkdcx_jgdh = $("#rkdcx-toolbar input[name='rkdcx_jgdh']").val();
    var rkdcx_khbm = $("#rkdcx-toolbar input[name='rkdcx_khbm']").val();
    var sftzd = $("#rkdcx-toolbar input[name='sftzd']").val();
    $('#rkdcx-grid').datagrid('load',
        {
            sftzd:sftzd,
            jgdh:rkdcx_jgdh,
            khbm:rkdcx_khbm
        }
    );
}
